#include "laurin-ip-network.h"


LaurinIPNetwork::LaurinIPNetwork(u_int32_t localAddress, u_int32_t subnetMask)
{
	_local_address = localAddress;
	_subnet_mask = subnetMask;

	_range = LaurinIPRange::getIPRange(localAddress, subnetMask);
}


LaurinIPNetwork::~LaurinIPNetwork()
{
	delete _range;
	removeAll();
}


LaurinIPRange* LaurinIPNetwork::getIPRange() const
{
	return new LaurinIPRange(*_range);
}


u_int32_t LaurinIPNetwork::getLocalAddress() const
{
	return _local_address;
}


u_int32_t LaurinIPNetwork::getSubnetMask() const
{
	return _subnet_mask;
}


bool LaurinIPNetwork::addClient(u_int32_t ip, u_int8_t* mac, bool check)
{
	return addClient(LaurinIPNetworkClient(ip, mac), check);
}


bool LaurinIPNetwork::addClient(const LaurinIPNetworkClient& client, bool check)
{
	if(check && isClientPresent(client.getIP()))
		return false;

	_clients.push_front(client);
	return true;
}


void LaurinIPNetwork::removeAll()
{
	_clients.clear();
}


LaurinIPNetworkClient* LaurinIPNetwork::isClientPresent(u_int32_t ip)
{
	std::list<LaurinIPNetworkClient>::iterator b = _clients.begin();
	std::list<LaurinIPNetworkClient>::iterator e = _clients.end();

	while(b != e)
	{
		if(b->getIP() == ip)
			return &(*b);

		b++;
	}

	return NULL;
}


std::list<LaurinIPNetworkClient>* LaurinIPNetwork::getClients()
{
	return &_clients;
}
